﻿package cn.antscript.airCMS.display.component.btnbase 
{
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author 
	 */
	public class AbstractBtn extends Sprite
	{
		private static var _format:TextFormat = new TextFormat(null, 14, 0x422424);
		private static var _outFilters:Array = [new DropShadowFilter(2, 45, 0x000000, 1, 3, 3), new BevelFilter(2)];
		private static var _overFilters:Array = [new DropShadowFilter(2, 40, 0x000000, 1, 5, 5), new BevelFilter(4)];
		
		
		protected var _txt:TextField;
		protected var _container:Sprite;
		
		public var data:AbstractData;
		public var isImg:Boolean;
		
		public function AbstractBtn(data_:AbstractData=null,isImg_:Boolean=false,xPos:int=0,yPos:int=0)
		{
			this.x = xPos;
			this.y = yPos;
			
			this.data = data_;
			this.isImg = isImg_;
			
			
			_container = new Sprite();
			_container.x = 10;
			_container.y = 10;
			addChild(_container);
			
			
			if (isImg) {
				
			}
			else {
				_txt = new TextField();
				_txt.width = 400;
				_txt.height = 30;
				_txt.text = data.label;
				_txt.setTextFormat(_format);
				_txt.selectable = false;
				_container.addChild(_txt);
				
				this.graphics.beginFill(0xE3B602);
				this.graphics.drawRoundRect(0, 0, _txt.width+20, 40, 3, 3);
				this.graphics.endFill();
			}
			
			
			
			
			this.filters = _outFilters;
			
			
			this.mouseChildren = false;
			this.buttonMode = true;
			
			this.addEventListener(MouseEvent.ROLL_OVER, onOver);
			this.addEventListener(MouseEvent.ROLL_OUT, onOut);
			this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		private function onDown(e:MouseEvent):void 
		{			
			this.filters = null;
			
			_container.x = 11;
			_container.y = 11;
			
			
			
			removeEventListener(Event.ENTER_FRAME, onEnter);
		}		
		private function onUp(e:MouseEvent):void 
		{
			
			this.filters = _overFilters;
			
			
			_container.x = 10;
			_container.y = 10;
			
			
		}
		
		private function onOver(e:MouseEvent):void 
		{
			this.filters = _overFilters;
			
			_container.x = 9;
			_container.y = 9;
			
			if (!isImg) {
				if (_txt.width < _txt.textWidth) {
					addEventListener(Event.ENTER_FRAME, onEnter);
				}				
			}
			
		}
		
		private function onEnter(e:Event):void
		{
			
			_txt.scrollH += 1;
			if (_txt.scrollH == _txt.maxScrollH) {
				removeEventListener(Event.ENTER_FRAME, onEnter);
			}
		}
		
		private function onOut(e:MouseEvent):void 
		{
			_container.x = 10;
			_container.y = 10;
			
			if (!isImg) {
				_txt.scrollH = 0;
			}
			
			this.filters = _outFilters;
			removeEventListener(Event.ENTER_FRAME, onEnter);
		}
		
		
		public function setSize(w:int, h:int):void {
			
			if (!isImg) {
				_txt.width = w-20;
			}
			
			
			this.graphics.beginFill(0xE3B602);
			this.graphics.drawRoundRect(0, 0, w, h, 3, 3);
			this.graphics.endFill();
		}
		
	}

}